package com.storyroad.responseClasses;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Fields of this class are used to store information about a story fragment. Instances of this class are sent to the front-end
 * to be used when viewing a story. Accumulating all the data in an object facilitates transfer of data as well as receiving and
 * processing it. 
 */
public class Story {
	// database columns
	private int s_id;
	private String date;
	private String title;
	private String location;
	private int owner;
	private int parent;
	private double x_coord;
	private double y_coord;
	private String body;
	private String media;
	private int totalRating;
	private int ratingCount;
	private String allowedList;
	private String tags;

	// intended fields
	private String userName;
	private String photoLink;
	private int myRating;
	private ArrayList<Comment> comments;
	private Map<Integer, String> storyTitleMap;
	private Map<Integer, String> storyBodyMap;
	private Map<Integer, String> storyUserMap;
	private Map<Integer, Coordinates> storyCoordinateMap;
	private ArrayList<StoryLinkObject> storyLinks;

	public Story() {
		storyTitleMap = new HashMap<Integer, String>();
		storyBodyMap = new HashMap<Integer, String>();
		setStoryCoordinateMap(new HashMap<Integer, Coordinates>());
		setStoryUserMap(new HashMap<Integer, String>());
	}

	/**
	 * Fills out the class fields using the information retrieved from the DB.
	 * @param resultSet ResultSet object retrieved from the DB
	 */
	public void createStoryObjectFromResultSet(ResultSet resultSet) {
		try {
			s_id = resultSet.getInt("s_id");
			Timestamp temp_date = resultSet.getTimestamp("date");
			title = resultSet.getString("title");
			location = resultSet.getString("location");
			owner = resultSet.getInt("owner");
			parent = resultSet.getInt("parent");
			x_coord = resultSet.getDouble("x_coord");
			y_coord = resultSet.getDouble("y_coord");
			body = resultSet.getString("body");
			media = resultSet.getString("media");
			totalRating = resultSet.getInt("totalRating");
			ratingCount = resultSet.getInt("ratingCount");
			allowedList = resultSet.getString("allowedList");

			SimpleDateFormat dt = new SimpleDateFormat("YYYY.MM.dd HH:mm.ss");
			date = dt.format(temp_date);

		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void addObjectToStoryTitleMap(int key, String value) {
		storyTitleMap.put(key, value);
	}

	public void addObjectToStoryBodyMap(int key, String value) {
		storyBodyMap.put(key, value);
	}

	public void addObjectToStoryUserMap(int key, String value) {
		storyUserMap.put(key, value);
	}

	public void addObjectToStoryCoordinateMap(int key, double x, double y) {
		Coordinates coordinate = new Coordinates(x, y);
		storyCoordinateMap.put(key, coordinate);
	}

	/** GETTERS & SETTERS **/

	public int getS_id() {
		return s_id;
	}

	public void setS_id(int s_id) {
		this.s_id = s_id;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public int getOwner() {
		return owner;
	}

	public void setOwner(int owner) {
		this.owner = owner;
	}

	public int getParent() {
		return parent;
	}

	public void setParent(int parent) {
		this.parent = parent;
	}

	public double getX_coord() {
		return x_coord;
	}

	public void setX_coord(double x_coord) {
		this.x_coord = x_coord;
	}

	public double getY_coord() {
		return y_coord;
	}

	public void setY_coord(double y_coord) {
		this.y_coord = y_coord;
	}

	public String getBody() {
		return body;
	}

	public void setBody(String body) {
		this.body = body;
	}

	public String getMedia() {
		return media;
	}

	public void setMedia(String media) {
		this.media = media;
	}

	public int getTotalRating() {
		return totalRating;
	}

	public void setTotalRating(int totalRating) {
		this.totalRating = totalRating;
	}

	public int getRatingCount() {
		return ratingCount;
	}

	public void setRatingCount(int ratingCount) {
		this.ratingCount = ratingCount;
	}

	public String getAllowedList() {
		return allowedList;
	}

	public void setAllowedList(String allowedList) {
		this.allowedList = allowedList;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getTags() {
		return tags;
	}

	public void setTags(String tags) {
		this.tags = tags;
	}

	public ArrayList<Comment> getComments() {
		return comments;
	}

	public void setComments(ArrayList<Comment> comments) {
		this.comments = comments;
	}

	public Map<Integer, String> getStoryTitleMap() {
		return storyTitleMap;
	}

	public void setStoryTitleMap(Map<Integer, String> storyTitleMap) {
		this.storyTitleMap = storyTitleMap;
	}

	public Map<Integer, String> getStoryBodyMap() {
		return storyBodyMap;
	}

	public void setStoryBodyMap(Map<Integer, String> storyBodyMap) {
		this.storyBodyMap = storyBodyMap;
	}

	public ArrayList<StoryLinkObject> getStoryLinks() {
		return storyLinks;
	}

	public void setStoryLinks(ArrayList<StoryLinkObject> storyLinks) {
		this.storyLinks = storyLinks;
	}

	public Map<Integer, String> getStoryUserMap() {
		return storyUserMap;
	}

	public void setStoryUserMap(Map<Integer, String> storyUserMap) {
		this.storyUserMap = storyUserMap;
	}

	public Map<Integer, Coordinates> getStoryCoordinateMap() {
		return storyCoordinateMap;
	}

	public void setStoryCoordinateMap(
			Map<Integer, Coordinates> storyCoordinateMap) {
		this.storyCoordinateMap = storyCoordinateMap;
	}

	public int getMyRating() {
		return myRating;
	}

	public void setMyRating(int myRating) {
		this.myRating = myRating;
	}

	public String getPhotoLink() {
		return photoLink;
	}

	public void setPhotoLink(String photoLink) {
		this.photoLink = photoLink;
	}

	public class Coordinates {
		private double x;
		private double y;

		public Coordinates(double x, double y) {
			this.setX(x);
			this.setY(y);
		}

		public double getX() {
			return x;
		}

		public void setX(double x) {
			this.x = x;
		}

		public double getY() {
			return y;
		}

		public void setY(double y) {
			this.y = y;
		}
	}
}
